/**
 * 
 */
package com.alpsoft.spotcheck.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.alpsoft.spotcheck.db.DBManager;
import com.alpsoft.spotcheck.db.SQLiteTemplate;
import com.alpsoft.spotcheck.db.SQLiteTemplate.RowMapper;
import com.alpsoft.spotcheck.model.Attachment;
import com.alpsoft.spotcheck.model.CheckRecord;

/**
 * @author zz.han 2015年5月19日 下午8:50:04
 */
public class DataHelper {
	DBManager dbManager;
	SQLiteTemplate st;

	public DataHelper(Context context) {
		this.dbManager = DBManager.getInstance(context, GlobalVar.databaseName);
		this.st = SQLiteTemplate.getInstance(dbManager, false);
	}

	public boolean hasLocalData() {
		Integer attNo = st.getCount("EQ_ATTACHMENT", null);
		Integer itemNo = st.getCount("EQ_CHECK_ITEM", null);
		Integer recordNo = st.getCount("EQ_CHECK_RECORD", null);
		Integer eqNo = st.getCount("EQ_EQUIPMENT", null);
		Integer expNo = st.getCount("RP_EXCEPTION_SUBMIT", null);
		Integer sum = attNo + itemNo + recordNo + eqNo + expNo;
		if (sum == 0) {
			return false;
		} else {
			return true;
		}
	}

	// 以下是获取数据以及清除资源的部分
	public Map<String, Object> getDataMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<CheckRecord> recordList = getRecordList();
		List<com.alpsoft.spotcheck.model.Exception> expList = getExpList();
		map.put("eq_check_record", recordList);
		map.put("eq_exception", expList);
		return map;
	}

	public List<CheckRecord> getRecordList() {
		List<CheckRecord> list = st.queryForList(new RowMapper<CheckRecord>() {

			@Override
			public CheckRecord mapRow(Cursor cursor, int index) {
				CheckRecord cr = new CheckRecord();
				cr.setCheckRecordId(cursor.getString(cursor
						.getColumnIndex("CHECK_RECORD_ID")));
				cr.setCheckItemStrategyId(cursor.getString(cursor
						.getColumnIndex("CHECK_ITEM_STRATEGY_ID")));
				cr.setCheckUserId(cursor.getString(cursor
						.getColumnIndex("CHECK_USER_ID")));
				cr.setCheckTime(cursor.getString(cursor
						.getColumnIndex("CHECK_TIME")));
				cr.setCheckResult(cursor.getString(cursor
						.getColumnIndex("CHECK_RESULT")));
				cr.setIsException(cursor.getString(cursor
						.getColumnIndex("IS_EXCEPTION")));
				cr.setPlanDate(cursor.getString(cursor
						.getColumnIndex("PLAN_DATE")));
				cr.setEndTime(cursor.getString(cursor
						.getColumnIndex("END_TIME")));
				cr.setStartTime(cursor.getString(cursor
						.getColumnIndex("START_TIME")));
				return cr;
			}
		}, "select * from EQ_CHECK_RECORD", null);
		return list;
	}

	public List<com.alpsoft.spotcheck.model.Exception> getExpList() {
		List<com.alpsoft.spotcheck.model.Exception> list = st.queryForList(
				new RowMapper<com.alpsoft.spotcheck.model.Exception>() {

					@Override
					public com.alpsoft.spotcheck.model.Exception mapRow(
							Cursor cursor, int index) {
						com.alpsoft.spotcheck.model.Exception exp = new com.alpsoft.spotcheck.model.Exception();
						exp.setExceptionSubmitId(cursor.getString(cursor
								.getColumnIndex("EXCEPTION_SUBMIT_ID")));
						exp.setSubmitUserId(cursor.getString(cursor
								.getColumnIndex("SUBMIT_USER_ID")));
						exp.setEquipmentId(cursor.getString(cursor
								.getColumnIndex("EQUIPMENT_ID")));
						exp.setResourceId(cursor.getString(cursor
								.getColumnIndex("RESOURCE_ID")));
						exp.setSubmitTime(cursor.getString(cursor
								.getColumnIndex("SUBMIT_TIME")));
						exp.setSubmitContent(cursor.getString(cursor
								.getColumnIndex("SUBMIT_CONTENT")));
						exp.setStatus(cursor.getString(cursor
								.getColumnIndex("STATUS")));
						exp.setDxResult(cursor.getString(cursor
								.getColumnIndex("DX_RESULT")));
						exp.setResourceType(cursor.getString(cursor
								.getColumnIndex("RESOURCE_TYPE")));
						exp.setDxTime(cursor.getString(cursor
								.getColumnIndex("DX_TIME")));
						exp.setRemarks(cursor.getString(cursor
								.getColumnIndex("REMARKS")));
						exp.setDxUserId(cursor.getString(cursor
								.getColumnIndex("DX_USER_ID")));
						exp.setFlag(cursor.getString(cursor
								.getColumnIndex("FLAG")));

						return exp;
					}
				}, "select * from RP_EXCEPTION_SUBMIT", null);
		return list;
	}

	public List<Attachment> getAttachList() {
		List<Attachment> list = st.queryForList(new RowMapper<Attachment>() {

			@Override
			public Attachment mapRow(Cursor cursor, int index) {
				Attachment att = new Attachment();
				att.setAttachmentId(cursor.getString(cursor
						.getColumnIndex("ATTACHMENT_ID")));
				att.setAttachmentName(cursor.getString(cursor
						.getColumnIndex("ATTACHMENT_NAME")));
				att.setFilePath(cursor.getString(cursor
						.getColumnIndex("FILE_PATH")));
				att.setTableName(cursor.getString(cursor
						.getColumnIndex("TABLE_NAME")));
				att.setKey(cursor.getString(cursor.getColumnIndex("KEY")));
				att.setValue(cursor.getString(cursor.getColumnIndex("VALUE")));

				return att;
			}
		}, "select * from EQ_ATTACHMENT", null);
		return list;
	}

	public void clear_dj() {
		clearData_dj();
		clearPic();
	}

	public void clearData_dj() {

		SQLiteDatabase db = dbManager.openDatabase();

		try {
			db.beginTransaction();
			db.execSQL("delete from EQ_CHECK_ITEM");
			db.execSQL("delete from EQ_CHECK_RECORD");
			db.execSQL("delete from EQ_EQUIPMENT");
			db.execSQL("delete from RP_EXCEPTION_SUBMIT");
			db.execSQL("DELETE FROM sqlite_sequence WHERE name = 'EQ_CHECK_ITEM' ");
			db.execSQL("DELETE FROM sqlite_sequence WHERE name = 'EQ_CHECK_RECORD' ");
			db.execSQL("DELETE FROM sqlite_sequence WHERE name = 'EQ_EQUIPMENT' ");
			db.execSQL("DELETE FROM sqlite_sequence WHERE name = 'RP_EXCEPTION_SUBMIT' ");
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != db) {
				db.close();
			}
		}
	}

	public void clearPic() {

		SQLiteDatabase db = dbManager.openDatabase();
		try {
			db.beginTransaction();
			db.execSQL("delete from EQ_ATTACHMENT");
			db.execSQL("DELETE FROM sqlite_sequence WHERE name = 'EQ_ATTACHMENT' ");
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
			FileUtil.delFolder(GlobalVar.picPath);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != db) {
				db.close();
			}
		}
	}
}
